<template>
    <div class="sys-dictType-container">
        <el-dialog v-model="state.isShowDialog" draggable :close-on-click-modal="false" width="700px">
            <template #header>
                <div style="color: #fff">
                    <el-icon size="16" style="margin-right: 3px; display: inline; vertical-align: middle"> <ele-Edit />
                    </el-icon>
                    <span> {{ props.title }} </span>
                </div>
            </template>
            <el-form :model="state.ruleForm" ref="ruleFormRef" label-width="auto">
                <el-row :gutter="35">
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
                        <el-form-item label="仓库" prop="warehousCode"
                            :rules="[{ required: true, message: '请选择仓库', trigger: 'blur' }]">
                            <el-select v-model="state.ruleForm.warehousCode" placeholder="请选择仓库">
                                <el-option v-for="item in state.warehouseList" :key="item.code" :label="item.name"
                                    :value="item.code">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
                        <el-form-item label="库位" prop="code"
                            :rules="[{ required: true, message: '库位代码不能为空', trigger: 'blur' }]">
                            <el-input v-model="state.ruleForm.code" placeholder="库位" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
                        <el-form-item label="点位" prop="poiint"
                            :rules="[{ required: true, message: '库位点位不能为空', trigger: 'blur' }]">
                            <el-input v-model="state.ruleForm.poiint" placeholder="点位" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
                        <el-form-item label="通道点位" prop="channelLocation"
                            :rules="[{ required: true, message: '通道点位不能为空', trigger: 'blur' }]">
                            <el-input v-model="state.ruleForm.channelLocation" placeholder="通道点位" clearable />
                        </el-form-item>
                    </el-col>

           
                        <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
                            <el-form-item label="库位楼层" prop="storey"
                                :rules="[{ required: true, message: '请选择楼层', trigger: 'change' }]">
                                <el-select v-model="state.ruleForm.storey" placeholder="请选择">
                                    <el-option v-for="item in [1, 2, 3, 4]" :key="item" :label="item.toString()"
                                        :value="item">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
       

                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
                        <el-form-item label="冻结" prop="isEnable"
                            :rules="[{ required: false, message: '必选', trigger: 'blur' }]">
                            <!-- v-if="state.ruleForm.name != null" 加在是否的item上 -->
                            <el-radio-group v-model="state.ruleForm.isenable">
                                <el-radio :value="0">是</el-radio>
                                <el-radio :value="1">否</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <!-- <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
                        <el-form-item label="备注" prop="">
                            <el-input v-model="state.ruleForm" placeholder="请输入备注内容" clearable type="textarea" />
                        </el-form-item>
                    </el-col> -->
                </el-row>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="cancel">取 消</el-button>
                    <el-button type="primary" @click="submit">确 定</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script lang="ts" setup name="EditPosition">


import { AccountTypeEnum, UpdateDictTypeInput } from '/@/api-services/models';
import { useUserInfo } from '/@/stores/userInfo';
import { WarehousePositionApi } from '/@/api-services/apis/warehouse-position-api'; // 导入 API 服务
import { WarehousePosition, DeleteWarehousePositionInput } from '/@/api-services/models/warehouse-position-input'; // 导入获取实体
// 假设从 API 获取仓库数据，先导入必要的函数
import { ref, onMounted, reactive } from 'vue';
import { getAPI } from '/@/utils/axios-utils';
import { ElMessageBox, ElMessage } from 'element-plus';

import { WarehouseInput } from '/@/api-services/models/warehouse-input'; // 导入仓库实体添加编辑下拉
import { WarehouseApi } from '/@/api-services/apis/warehouse-api';




const ruleFormRef = ref();
const state = reactive({
    loading: false,
    isShowDialog: false,
    ruleForm: {} as WarehousePosition,
    warehouseList: Array<WarehouseInput>(),

});


// 在 onMounted 生命周期钩子中获取仓库数据
onMounted(async () => {
    try {
        state.loading = true;
        // 调用 API 获取仓库数据
        const response = await getAPI(WarehouseApi).apiWarehousePagePost({
            page: 1,
            pageSize: 1000, isEnable: 1
        });
        state.warehouseList = response.data.result?.items || [];
        console.log(state.warehouseList);
    } catch (error) {
        console.error('获取仓库数据失败', error);
    }
});

// 打开弹窗
const openDialog = (row: any) => {
    state.ruleForm = JSON.parse(JSON.stringify(row));
    state.isShowDialog = true;
    ruleFormRef.value?.resetFields();
};

const props = defineProps({
    title: String,
});
const emits = defineEmits(['handleQuery']);


// 关闭弹窗
const closeDialog = () => {
    emits('handleQuery');
    state.isShowDialog = false;
};

// 取消
const cancel = () => {
    state.isShowDialog = false;
};

// 提交
const submit = () => {
    ruleFormRef.value.validate(async (valid: boolean) => {
        if (!valid) return;
        if (state.ruleForm.id != undefined && state.ruleForm.id > 0) {

            await getAPI(WarehousePositionApi).apiWarehousePositionUpdatePost(state.ruleForm);
            ElMessage({
                message: '修改成功',
                type: 'success',
            })
        } else {
            await getAPI(WarehousePositionApi).apiWarehousePositionAddPost(state.ruleForm);
            ElMessage({
                message: '添加成功',
                type: 'success',
            })
        }
        closeDialog();
    });
};

// 导出对象
defineExpose({ openDialog });
</script>
